//
//  UPMainViewController.m
//  UESTCPioneer
//
//  Created by Sway on 14-3-5.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "UPMainViewController.h"

@interface UPMainViewController (){
    UIButton *barButton;
    
    UIView *dropdownview;
}

@end


@implementation UPMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    if(IS_IOS7)
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    [self.tableView setAllowsSelection:NO];
    
    [self.view addSubview:dropdownview];
    
    //界面显示，监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationUnreadrefresh:) name:kNotificationUnreadTotalRefreshed object:nil];
}

-(void)viewWillAppear:(BOOL)animated{
//    [self _initBarButton];
}

- (void)dropClicked:(id)sender{}
- (void)hideTopView{
    dropdownview.hidden = YES;
    self.tableView.frame = CGRectMake(10, 5, 300,445);
}
- (void) niDropDownDelegateMethod: (NIDropDown *) sender {
    [self rel];
}

- (void)rel{
    //[dropDown release];
    dropDown = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)leftBarButtonAction:(UIButton*)button{
    
    [self.leveyTabBarController.revealSideViewController pushOldViewControllerOnDirection:PPRevealSideDirectionLeft animated:YES];
    
}

///能根据未读消息自动刷新或生成并显示一个barButtonItem
-(void)_initBarButton{
    if (barButton==nil){
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        b.contentMode = UIViewContentModeScaleAspectFit;
        [b addTarget:self action:@selector(leftBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        barButton=b;
    }
    //    NSInteger unreadTotal=[Unread getUnreadNumWithKey:kUnreadTotalKey];
    NSInteger unreadTotal= [Unread getUnreadNumWithKey:kUnreadPioneerKey] + [Unread getUnreadNumWithKey:kUnreadPartyNoticeKey];
    if (unreadTotal){
        [barButton setImage:[UIImage imageNamed:@"menu_msg.png"] forState:UIControlStateNormal];
        [barButton setImageEdgeInsets:UIEdgeInsetsMake(12, 12, 12, 42)];
    } else {
        [barButton setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
        [barButton setImageEdgeInsets:UIEdgeInsetsMake(12, 12, 12, 42)];
    }
    
    UIBarButtonItem *barItem=[helper BarButtonItemWithUIButton:barButton ButtonOrigin:CGPointMake(0, 0)];
    [self.leveyTabBarController.navigationItem setLeftBarButtonItem:barItem];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(235, 0, 110, 44)];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(8, 0, 8, 25)];
    [btn.titleLabel setTextAlignment:NSTextAlignmentLeft];
    NSString *title = @"全部";
    
    if ([constant getCurrentView] == 2) {
        title = @"最新";
    }
    
    [btn setTitle:title forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
    btn.tintColor = [UIColor whiteColor];
    [btn addTarget:self action:@selector(dropClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *xiala = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiala.png"]];
    xiala.contentMode = UIViewContentModeScaleAspectFit;
    xiala.frame = CGRectMake(88, 8+9, 12, 12);
    xiala.tag = 22;
    [btn addSubview:xiala];
    
    dropbtn = btn;
    UIBarButtonItem *barItem2=[helper BarButtonItemWithUIButton:btn ButtonOrigin:CGPointMake(0, 0) CustomViewSize:CGSizeMake(110, 44)];

    self.leveyTabBarController.navigationItem.rightBarButtonItem = barItem2;
}

#pragma mark - Notification

//未读消息有更新
-(void)notificationUnreadrefresh:(NSNotification*)notify{
//    [self _initBarButton];
    
    if ([notify.name isEqualToString:kNotificationUnreadTotalRefreshed]) {
        //当前控制器是否有新值
        //    NSInteger unread= [Unread getUnreadNumWithKey:UnreadKey];
        NSInteger unread= [Unread getUnreadNumWithKey:kUnreadPioneerKey] + [Unread getUnreadNumWithKey:kUnreadPartyNoticeKey];
        
        NSDictionary *dic;
        //    NSLog(@"unread = %i",unread);
        if (unread){
            dic= @{@"Default":[UIImage imageNamed:@"home_msg"],@"Highlighted":[UIImage imageNamed:@"home_highlighted_msg"],@"Seleted":[UIImage imageNamed:@"home_highlighted_msg"]};
        }
        else {
            dic=@{@"Default":[UIImage imageNamed:@"home"],@"Highlighted":[UIImage imageNamed:@"home_highlighted"],@"Seleted":[UIImage imageNamed:@"home_highlighted"]};
        }
        [self.leveyTabBarController setTabBarItemWithImageDicationary:dic ForIndex:0];
    }else if([notify.name isEqualToString:kLoginNotifaction]){
        NSDictionary *dic;
        dic=@{@"Default":[UIImage imageNamed:@"home"],@"Highlighted":[UIImage imageNamed:@"home_highlighted"],@"Seleted":[UIImage imageNamed:@"home_highlighted"]};
        [self.leveyTabBarController setTabBarItemWithImageDicationary:dic ForIndex:0];
    }
    //    NSLog(@"notify.name = %@",notify.name);
}



@end
